import http from "../../utils/request";

Page({
  data: {
    //   标记点
    markers: [
      {
        longitude: "113.88308",
        latitude: "22.55329",
        iconPath: "/images/location.png",
        width: "48",
        height: "48",
      },
      {
        longitude: "113.883741",
        latitude: "22.555168",
        iconPath: "/images/location.png",
        width: "48",
        height: "48",
      },
    ],
    //  路线
    polyline: [
      //   {
      //     width: 4,
      //     color: "#ff0000",
      //     points: [
      //       { longitude: "113.880895", latitude: "22.557369" },
      //       { longitude: "113.886427", latitude: "22.558652" },
      //     ],
      //   },
    ],
  },

  onLoad() {
    this.getWalkingRoutes("22.557369,113.880895", "22.558652,113.886427");
  },

  // 获取步行路线规划
  async getWalkingRoutes(from, to) {
    const resp = await http({
      url: "https://apis.map.qq.com/ws/direction/v1/walking/",
      data: {
        from,
        to,
        key: "W6ABZ-T4Q65-7BWIH-IXR4V-TXG5Z-C4FJW",
      },
    });
    // 获取到 polyline
    let polyline = resp?.result?.routes?.[0]?.polyline;
    // 解压 polyline, 算法
    polyline = this.jieya(polyline);
    console.log(polyline);
    // 将 polyline 转换成 points
    const points = [];
    for (var i = 0; i < polyline.length; i += 2) {
      points.push({ latitude: polyline[i], longitude: polyline[i + 1] });
    }
    // setData polyline
    this.setData({
        polyline: [
            {
                width: 4,
                color: '#ff0000',
                points
            }
        ]
    })
  },

  // 腾讯地图 polyline 解压算法
  jieya(coors) {
    for (var i = 2; i < coors.length; i++) {
      coors[i] = coors[i - 2] + coors[i] / 1000000;
    }
    return coors;
  },
});
